package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfSpaceEntity;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfSpaceVO;
import com.wing.warehouse.model.request.WarehouseFactoryShelfSpaceForm;
import com.wing.warehouse.controller.admin.service.WarehouseFactoryShelfSpaceService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@Api(value = "仓库货架货位管理", tags = "#/warehouseFactory/warehouseFactoryShelfSpace")
@RestController("WarehouseFactoryShelfSpaceController")
@RequestMapping("warehouse/admin/warehouseFactoryShelfSpace")
@RequiredArgsConstructor
public class WarehouseFactoryShelfSpaceController {

    private final WarehouseFactoryShelfSpaceService warehouseShelfSpaceService;


    @ApiOperation(value = "查询所有仓库货架货位", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseFactoryShelfSpaceEntity> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "999") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String code,Long shelfId,Long warehouseId,Long warehouseAreaId){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("shelfId",shelfId);
        queryParam.put("warehouseId",warehouseId);
        queryParam.put("warehouseAreaId",warehouseAreaId);
        if (shelfId!=null){
            queryParam.put("sort",StrUtil.toUnderlineCase("code"));
            queryParam.put("order","asc");
        }
        queryParam.put("status", WarehouseFactoryShelfSpaceEntity.Status.enable.getValue());

        IPage<WarehouseFactoryShelfSpaceEntity> list = warehouseShelfSpaceService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加仓库货架货位", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseFactoryShelfSpaceForm.Add.class) WarehouseFactoryShelfSpaceForm warehouseShelfSpaceForm) {
        if (warehouseShelfSpaceService.add(warehouseShelfSpaceForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "添加仓库货架货位", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addFlat")
    public JsonResult addFlat(@Validated(WarehouseFactoryShelfSpaceForm.Add.class) WarehouseFactoryShelfSpaceForm warehouseShelfFlatSpaceForm) {
        return warehouseShelfSpaceService.addFlat(warehouseShelfFlatSpaceForm);
    }

    @ApiOperation(value = "修改仓库货架货位", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseFactoryShelfSpaceForm.Update.class) WarehouseFactoryShelfSpaceForm warehouseShelfSpaceForm) {
        WarehouseFactoryShelfSpaceEntity warehouseShelfSpace = warehouseShelfSpaceService.getById(warehouseShelfSpaceForm.getId());
        if(warehouseShelfSpace == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseFactoryShelfSpaceEntity.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseShelfSpaceForm, warehouseShelfSpace, copyOptions);
        warehouseShelfSpace.setCode(warehouseShelfSpaceForm.getFirstCode());
        if (warehouseShelfSpaceService.updateById(warehouseShelfSpace)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "删除仓库货架货位", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseShelfSpaceId", value = "仓库货架货位id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long warehouseShelfSpaceId) {
        if (warehouseShelfSpaceService.removeById(warehouseShelfSpaceId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @GetMapping("/findByCode")
    public JsonResult findByCode(String code){
        WarehouseFactoryShelfSpaceVO warehouseShelfSpaceVO = warehouseShelfSpaceService.findByCode(code);
        if(warehouseShelfSpaceVO == null){
            return JsonResult.fail("货位号不存在");
        }
        return JsonResult.success(warehouseShelfSpaceVO);
    }

    @ApiOperation(value = "查询采购入库产品库位", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "skuCode编号", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "warehouseId", value = "仓库id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getShelfSpace")
    public JsonResult<List<WarehouseFactoryShelfSpaceVO>> getShelfSpace(String skuCode, Long warehouseId){
        List<WarehouseFactoryShelfSpaceVO> warehouseShelfSpaceVOList = warehouseShelfSpaceService.getShelfSpace(skuCode, warehouseId);
        return JsonResult.success(warehouseShelfSpaceVOList);
    }

    @GetMapping("/selectProductSpace")
    public JsonResult selectProductSpace(String id){
        List<WarehouseFactoryShelfSpaceEntity> warehouseFactoryShelfSpaceEntity = warehouseShelfSpaceService.selectProductSpace(id);
            return JsonResult.success(warehouseFactoryShelfSpaceEntity);
    }
}
